package com.fucking.great.tomcat;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

@SuppressWarnings("serial")
@WebServlet("/myServlet12312")
public class MyServlet extends HttpServlet {
	public static final Log log = LogFactory.get();
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/json; charset=utf-8");
		String tt = request.getParameter("tt");
		System.out.println(tt);
		System.out.println(request.getRequestURI());
		System.out.println(request.getPathInfo());

		Method[] gets = ReflectUtil.getMethods(ClassUtil.getClass(request), (filter) -> {
			String name = filter.getName();
			if (name.startsWith("get") && !"getInputStream".equals(name) && filter.getParameterCount() == 0) {
				return true;
			} else {
				return false;
			}
		});
		System.out.println(StrUtil.join(",",gets));
		log.error("类为[{}]的对象[{}]的get方法执行结果length[{}]", ClassUtil.getClassName(request,false), request,gets.length);
		for (Method get : gets) {
			try {
				log.error("方法名为[{}],内容为[{}]",get.getName() , ReflectUtil.invoke(request,get) );
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}

		PrintWriter printWriter = response.getWriter();
		printWriter.write("dsfa访问成功");
		printWriter.flush();
		printWriter.close();
		System.out.println("访问成功");
	}
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}
}
